﻿using NationalInstruments.DAQmx;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 低频天线阵列相控阵信号发生系统SmartFASK12.Controls;
using 低频天线阵列相控阵信号发生系统SmartFASK12.Models;
using 低频天线阵列相控阵信号发生系统SmartFASK12.Properties;

namespace 低频天线阵列相控阵信号发生系统SmartFASK12.View.AdjustDemodulation.Views
{
    public partial class Uc_Demodulation : UserControl
    {
        /// <summary>
        /// 采集是否启动
        /// </summary>
        private bool _isStart_Gather = false;
        public bool IsStart_Gather
        {
            get => _isStart_Gather;
            set
            {
                _isStart_Gather = value;
                if (_isStart_Gather)
                {
                    btn_StartAcquisition.Enabled = false;
                    btn_StopGather.Enabled = true;
                    panel_status_Gather.BackgroundImage = Resources.green_36;
                }
                else
                {
                    btn_StartAcquisition.Enabled = true;
                    btn_StopGather.Enabled = false;
                    panel_status_Gather.BackgroundImage = Resources.gray_36;
                }
            }
        }
        private int buValue;
        public Uc_Demodulation()
        {
            InitializeComponent();

            physicalChannelComboBox_gather.Items.AddRange(MNI.Singleton().GetChannals(PhysicalChannelTypes.AI));
            if (physicalChannelComboBox_gather.Items.Count > 0)
                physicalChannelComboBox_gather.SelectedIndex = 0;

            #region 采集
            minimumNumeric_gather.Value = (decimal)MPublic.ConfigModel2.minValue_gather;
            maximumNumeric_gather.Value = (decimal)MPublic.ConfigModel2.maxValue_gather;
            comboBox_samplingrate.Text = MPublic.ConfigModel2.samplesPerBuffer_gather;
            comboBox_samples.Text = MPublic.ConfigModel2.cyclesPerBuffer_gather;
            #endregion
            #region 调节
            comboBox_DemodulationSystem.SelectedIndex = MPublic.ConfigModel2.commissioningmode;
            comboBox_signalfrequency.Text = MPublic.ConfigModel2.signalfrequency;
            comboBox_samplingrate.Text = MPublic.ConfigModel2.samplingrate;
            comboBox_Carrierfrequency1.Text = MPublic.ConfigModel2.carrierfrequency1;
            comboBox_Carrierfrequency2.Text = MPublic.ConfigModel2.carrierfrequency2;
            comboBox_Amplitude1.Text = MPublic.ConfigModel2.amplitude1;
            comboBox_Amplitude2.Text = MPublic.ConfigModel2.amplitude2;
            comboBox_Filtercoefficient1.Text = MPublic.ConfigModel2.filtercoefficient1;
            comboBox_Filtercoefficient2.Text = MPublic.ConfigModel2.filtercoefficient2;
            comboBox_shifting.Text = MPublic.ConfigModel2.shifting;
            comboBox_Showpoints.Text = MPublic.ConfigModel2.showpoints;
            checkBox_gyh.Checked = MPublic.ConfigModel2.gyh;
            checkBox_showJudgePoint.Checked = MPublic.ConfigModel2.showJudgePoint;
            checkBox_zdbcdygz.Checked = MPublic.ConfigModel2.zdbcdygz;
            #endregion
        }

        /// <summary>
        /// 自动补充1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_bu1_Click(object sender, EventArgs e)
        {
            buValue = 1;
            btn_bu1.BackColor = Color.Lime;
            btn_bu0.BackColor = Color.White;
        }

        /// <summary>
        /// 自动补充0
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_bu0_Click(object sender, EventArgs e)
        {
            buValue = 0;
            btn_bu1.BackColor = Color.White;
            btn_bu0.BackColor = Color.Lime;
        }

        /// <summary>
        /// 开始采集
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_StartAcquisition_Click(object sender, EventArgs e)
        {
            SaveConfig();

            MEvent.Singleton().EmitStartCollection(true);
        }

        /// <summary>
        /// 停止采集
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_StopGather_Click(object sender, EventArgs e)
        {
            MEvent.Singleton().EmitStopAcquisition();
        }

        /// <summary>
        /// 解调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_demodulation_Click(object sender, EventArgs e)
        {
            SaveConfig();
            MEvent.Singleton().EmitDemodulation();
        }

        private void comboBox_Showpoints_TextChanged(object sender, EventArgs e)
        {
            MPublic.ConfigModel2.showpoints = comboBox_Showpoints.Text;
            MEvent.Singleton().EmitUpdataShowPoint();
        }

        /// <summary>
        /// 保存Config文件
        /// </summary>
        public void SaveConfig()
        {
            ConfigModel2 ConfigModel2 = MPublic.ConfigModel2;
            ConfigModel2.physicalChannel_gather = physicalChannelComboBox_gather.Text;
            #region 采集
            ConfigModel2.minValue_gather = Convert.ToDouble(minimumNumeric_gather.Value);
            ConfigModel2.maxValue_gather = Convert.ToDouble(maximumNumeric_gather.Value);
            ConfigModel2.samplesPerBuffer_gather = comboBox_samplingrate.Text;
            ConfigModel2.cyclesPerBuffer_gather = comboBox_samples.Text;
            ConfigModel2.startShowpoints = comboBox_StartShowpoints.Text;
            #endregion
            #region 调节
            ConfigModel2.commissioningmode = comboBox_DemodulationSystem.SelectedIndex;
            ConfigModel2.signalfrequency = comboBox_signalfrequency.Text;
            ConfigModel2.samplingrate = comboBox_samplingrate.Text;
            ConfigModel2.carrierfrequency1 = comboBox_Carrierfrequency1.Text;
            ConfigModel2.carrierfrequency2 = comboBox_Carrierfrequency2.Text;
            ConfigModel2.amplitude1 = comboBox_Amplitude1.Text;
            ConfigModel2.amplitude2 = comboBox_Amplitude2.Text;
            ConfigModel2.filtercoefficient1 = comboBox_Filtercoefficient1.Text;
            ConfigModel2.filtercoefficient2 = comboBox_Filtercoefficient2.Text;
            ConfigModel2.shifting = comboBox_shifting.Text;
            ConfigModel2.showpoints = comboBox_Showpoints.Text;
            ConfigModel2.gyh = checkBox_gyh.Checked;
            ConfigModel2.showJudgePoint = checkBox_showJudgePoint.Checked;
            ConfigModel2.zdbcdygz = checkBox_zdbcdygz.Checked;
            ConfigModel2.midValue = comboBox_midValue.Text;
            ConfigModel2.checkValue = comboBox_checkValue.Text;
            ConfigModel2.buValue = buValue;
            ConfigModel2.continuousquantity = comboBox_ContinuousQuantity.Text;
                #endregion
            MPublic.ConfigModel2 = ConfigModel2;
        }

        /// <summary>
        /// 显示抽样判决点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBox_showJudgePoint_CheckedChanged(object sender, EventArgs e)
        {
            MPublic.ConfigModel2.showJudgePoint = checkBox_showJudgePoint.Checked;
            MEvent.Singleton().EmitShowJudgePoint();
        }

        /// <summary>
        /// 显示点数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox_Showpoints_SelectedIndexChanged(object sender, EventArgs e)
        {
            MPublic.ConfigModel2.showpoints = comboBox_Showpoints.Text;
            MEvent.Singleton().EmitUpdataShowPoint();
        }

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_ExcelOut_Click(object sender, EventArgs e)
        {
            MEvent.Singleton().EmitExcelOut();
        }

        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Import_Click(object sender, EventArgs e)
        {
            MEvent.Singleton().EmitImportExcel();
        }
    }
}
